package com.easyaop.test.core.demo.annotation;

import com.easyaop.test.core.annotation.Log;
import com.easyaop.test.core.entity.Bean;
import com.easyaop.test.core.intercept.VirtualMethodAdvice;
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.matcher.ElementMatchers;

import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;

/**
 * @author lipan
 * @since 2025-01-20
 */
public class ByteBuddyTestAnnotation {

    public static void main(String[] args) throws UnmodifiableClassException {


        Instrumentation instrumentation = ByteBuddyAgent.install();
        Bean bean2 = new Bean();
        bean2.setName("name1");
        new AgentBuilder.Default()
                .with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION) // 启用重定义策略
                .disableClassFormatChanges()
                .type(ElementMatchers.any())
                .transform(
                        (builder, typeDescription, classLoader, javaModule, protectionDomain)
                                ->
                                builder.visit(
                                        Advice.to(VirtualMethodAdvice.class).on(ElementMatchers.isAnnotatedWith(Log.class)
                                        )
                                )
                )
//                .installOn(instrumentation);
                .installOnByteBuddyAgent();

        Bean bean = new Bean();
        bean.setName("my name");

//        instrumentation.retransformClasses(Bean.class);
//        bean.getName();
    }
}
